package handler

import (
	"github.com/gccai/write_helper/enum"
	"github.com/gccai/write_helper/model"
	"github.com/gccai/write_helper/resp"
	"github.com/gccai/write_helper/service"
	"github.com/gccai/write_helper/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

type DesignHandler struct {
	DesignSrv service.DesignServiceInterface
}

type DesignHandlerInterface interface {
	GetRespDesign(design model.Design) *resp.Design
	GetEntity() *resp.Entity
	GetCardContenHandler(c *gin.Context)
	GetAllCardHandler(c *gin.Context)
	GetOneTypeCardHandler(c *gin.Context)
	AddHandler(c *gin.Context)
	Edit(c *gin.Context)
	Delete(c *gin.Context)
}

func (d DesignHandler) GetRespDesign(design model.Design) *resp.Design {
	return &resp.Design{
		UserId:            design.UserId,
		BookName:          design.BookName,
		ChapterId:         design.ChapterId,
		DId:               design.DId,
		DesignAttribution: design.DesignAttribution,
		DType:             design.DType,
		DesignTitle:       design.DesignTitle,
		DesignContent:     design.DesignContent,
		CreateAt:          design.CreateAt,
	}
}
func (d DesignHandler) GetEntity() *resp.Entity {
	return &resp.Entity{
		Code: int(enum.OperateFail),
		Msg:  enum.OperateFail.String(),
		Data: nil,
	}
}

// localhost:8888/api/design/info?userId=?&&bookName=?&&dId=?
func (d DesignHandler) GetCardContenHandler(c *gin.Context) {
	var design model.Design
	entity := d.GetEntity()
	err := c.ShouldBindQuery(&design)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	dId, err := utils.CardNum2UUID(design.DId)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	design.DId = dId

	res, err := d.DesignSrv.GetCardContent(design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	respDesign := d.GetRespDesign(*res)
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	entity.Data = respDesign
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

//localhost:8888/api/desigin/list?userId=?&&bookName=?
func (d DesignHandler) GetAllCardHandler(c *gin.Context) {
	entity := d.GetEntity()
	var design model.Design
	err := c.ShouldBindQuery(&design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	results, err := d.DesignSrv.GetAllCard(design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	var respStruct resp.DesignTitleAndType
	var newList []interface{}
	for _, result := range *results {
		respStruct.UserId = result.UserId
		respStruct.BookName = result.BookName
		respStruct.ChapterId = result.ChapterId
		respStruct.DId = result.DId
		respStruct.DType = result.DType
		respStruct.DesignTitle = result.DesignTitle
		newList = append(newList, respStruct)
	}

	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	entity.Data = newList
	c.JSON(http.StatusOK, gin.H{"entity": entity})

}

//localhsot:8888/api/design/typeInfo?userId=?&&bookName=?&&designAttribution=?
func (d DesignHandler) GetOneTypeCardHandler(c *gin.Context) {
	entity := d.GetEntity()
	var design model.Design
	if err := c.ShouldBindQuery(&design); err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	// design.designAttribution 应该是int64类型，但是我有点不确定，放在这里，提醒自己一下。
	results, err := d.DesignSrv.GetOneTypeCard(design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	var respStruct resp.DesignTitleAndType
	var newList []interface{}
	for _, res := range *results {
		respStruct.UserId = res.UserId
		respStruct.BookName = res.BookName
		respStruct.ChapterId = res.ChapterId
		respStruct.DId = res.DId
		respStruct.DType = res.DType
		respStruct.DesignTitle = res.DesignTitle
		newList = append(newList, respStruct)
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	entity.Data = newList
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

func (d DesignHandler) AddHandler(c *gin.Context) {
	entity := d.GetEntity()
	var design model.Design
	err := c.ShouldBindJSON(&design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	b, err := d.DesignSrv.Add(design)
	if !b {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	resMap := make(map[string]string)
	resMap["add"] = "true"
	if b {
		entity.Code = int(enum.OperateOk)
		entity.Msg = enum.OperateOk.String()
		entity.Data = resMap
		c.JSON(http.StatusOK, gin.H{"entity": entity})
	}
}

//localhost:8888/api/design/edit
func (d DesignHandler) Edit(c *gin.Context) {
	entity := d.GetEntity()
	var design model.Design
	err := c.ShouldBindJSON(&design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	res, err := d.DesignSrv.Edit(design)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	entity.Data = res
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

//localhost:8888/api/design/delete?userId=?&&bookName=?&&dId=?
func (d DesignHandler) Delete(c *gin.Context) {
	entity := d.GetEntity()
	var design model.Design
	if err := c.ShouldBindQuery(&design); err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	b, err := d.DesignSrv.Delete(design)
	resMap := make(map[string]string)
	if !b {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	} else {
		entity.Code = int(enum.OperateOk)
		entity.Msg = enum.OperateOk.String()
		resMap["res"] = "true"
		entity.Data = resMap
		c.JSON(http.StatusOK, gin.H{"entity": entity})
	}
}
